package com.zlx.ssm.service.impl;

import com.zlx.ssm.mapper.EmployeeInfoMapper;
import com.zlx.ssm.model.EmployeeInfo;
import com.zlx.ssm.service.IEmpolyteService;
import com.zlx.ssm.service.TableDateServcie;
import com.zlx.ssm.vo.DateInfo;
import com.zlx.ssm.vo.ResultDateVo;
import com.zlx.ssm.vo.SelectVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class TableDateImpl implements TableDateServcie {

    @Autowired
    private IEmpolyteService iEmpolyteService;

    @Autowired
    private EmployeeInfoMapper employeeInfoMapper;

    @Override
    public ResultDateVo adnimDate(SelectVO selectVO) {

        ResultDateVo resultDateVo = new ResultDateVo();

        EmployeeInfo employeeInfo = new EmployeeInfo();

        employeeInfo.setName(selectVO.getName());
        employeeInfo.setSex(selectVO.getSex());
        employeeInfo.setCity(selectVO.getCity());
        employeeInfo.setRoleName(selectVO.getRoleName());
        //封装对象
        Integer page = selectVO.getPage();
        Integer limit = selectVO.getLimit();
        int start = (page - 1) * limit;

        //查询所有条数 赋值给条数
        int i = iEmpolyteService.selectAll();


        resultDateVo.setCode("0");
        resultDateVo.setMsg("");
        resultDateVo.setCount(String.valueOf(i));

        //遍历集合获取集合对象  封装
        List<DateInfo> dateInfoList = this.seleDateInfo(employeeInfo, start, limit);
        resultDateVo.setData(dateInfoList);

        //返回封装完的对象
        return resultDateVo;
    }


    public List<DateInfo> seleDateInfo(EmployeeInfo EmployeeInfo, int start, int limt) {

        //根据数据库查出所有对象
        List<EmployeeInfo> employeeInfos = employeeInfoMapper.updateByPrimaryAll(EmployeeInfo, start, limt);

        List<DateInfo> list = new ArrayList<>();

        //遍历集合进行封装
        for (EmployeeInfo employeeInfo : employeeInfos) {

            DateInfo dateInfo = new DateInfo();

            dateInfo.setId(String.valueOf(employeeInfo.getId()));
            dateInfo.setName(employeeInfo.getName());
            dateInfo.setPasswrod(employeeInfo.getPasswrod());
            dateInfo.setSex(employeeInfo.getSex());
            dateInfo.setRoleName(String.valueOf(employeeInfo.getRoleName()));
            dateInfo.setBirthday(employeeInfo.getBirthday());
            dateInfo.setCity(employeeInfo.getCity());
            list.add(dateInfo);
        }
        return list;
    }
}
